package service

import (
	"blog/app/models"
	"blog/utils"
	"bufio"
	"encoding/json"
	"fmt"
	"gorm.io/gorm"
	"log"
	"os"
	"strconv"
	"strings"
	"time"
)

func HelloDc() {
	//userModel := &models.UserBasic{}
	//fmt.Println(userModel.TableName())
	//for i := 1; i < 100; i++ {
	//	fmt.Println(utils.RangUserName())
	//}
	//
	fmt.Println(utils.ShenZhenRandAds())
}

// 随机生成地址信息
func addressDel() {
	// 读取文件
	content, err := os.ReadFile("doc/address.json")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(content))
	//oriJson := string(content)

}

// 随机生成用户插入MySQL和es
func randList() {
	userModel := &models.UserBasic{}
	fmt.Println(userModel.TableName())
	for i := 1; i < 2000; i++ {
		strNumber := strconv.Itoa(i)
		u := models.UserBasic{
			Model: gorm.Model{
				UpdatedAt: time.Now(),
				CreatedAt: time.Now(),
			},
			UserName:      utils.RangUserName(),
			Password:      "123456",
			Phone:         utils.RandPhone(),
			Email:         strNumber + "@qq.com",
			Identity:      strNumber,
			ClientIP:      "1.1",
			ClientPort:    "6000",
			LoginTime:     time.Now(),
			HeartbeatTime: time.Now(),
			LogOutTime:    time.Now(),
			IsLogout:      false,
		}
		utils.MySqlDB.Create(&u)

		kk, err := json.Marshal(u)
		if err != nil {
			fmt.Println("转换失败", err)
		}
		fmt.Println(string(kk))
		utils.AddEsDataString(userModel.TableName(), strconv.Itoa(i), string(kk))
		utils.ShowEsData(userModel.TableName(), strconv.Itoa(i))
	}
}

// 把文件数据添加末尾逗号
func dealPhone() {
	file, _ := os.Open("doc/shenzhen.txt")
	fileScanner := bufio.NewScanner(file)
	//lineCount := 1
	var rsStr []string
	for fileScanner.Scan() {
		//fmt.Println("文件", fileScanner.Text())
		rsStr = append(rsStr, fileScanner.Text())
	}
	defer file.Close()
	fmt.Println(rsStr)
	fmt.Println("dddd", strings.Join(rsStr, ","))

	//// 读取文件
	//content, err := os.ReadFile("doc/zh.txt")
	//if err != nil {
	//	log.Fatal(err)
	//}
	//fmt.Println(string(content))
	//dealPhone()
}
